// pages/my/shopdetail/shopdetail.js
let utils = require("../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperHeight: 440, // swiper高度
    shopInfo: null,
    shopImgFullPaths: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setSwiperHeight();
    that.getMyShop(options.shopid);
  },
  setSwiperHeight() {
    let that = this;
    wx.getSystemInfo({
      success: function (res) {
        let winW = res.windowWidth;
        let imgradio = 750 / 440;
        let imgH = winW / imgradio;
        that.setData({
          swiperHeight: imgH
        })
      }
    })
  },
  getMyShop: function (shopid) {
    var that = this;
    var shopImg = [];
    utils.getData(
      utils.apiUrl + "/shop/GetShopDetail?shopId=" + shopid,
      {},
      function (response) {
        for (let i = 0; i < response.data.ShopImgs.length; i++) {
          shopImg.push(response.data.ShopImgs[i]);
        }
        that.setData({
          shopInfo: response.data,
          shopImgFullPaths: shopImg
        });
      }
    );
  },
  // 拨打电话  
  bind_tal: function () {
    var that = this;
    utils.postData(
      utils.apiUrl + "/message/AddPhoneCall",
      {
        OwnerShopID: that.data.shopInfo.ShopID,
        PostType: 0,
        PostId: 0,
        CommentUserID: utils.getUserId(),
        FromType: 2
      },
      function () {
        wx.makePhoneCall({
          phoneNumber: that.data.shopInfo.Mobile
        });
      }
    );
  },
  previewImage: function (e) {
    var that = this;
    var index = e.target.dataset.index;       
    var shopImgs = that.data.shopImgFullPaths.map(item => {
      return item.HttpImgUrl
    });
    wx.previewImage({
      current: shopImgs[index], // 当前显示图片的http链接  
      urls: shopImgs // 需要预览的图片http链接列表  
    })
  },
  //地图查看位置
  bindOpenLocation: function () {
    var that = this;
    var latitude = parseFloat(that.data.shopInfo.MapLatitude)
    var longitude = parseFloat(that.data.shopInfo.MapLogitude)
        wx.openLocation({
          latitude: latitude,
          longitude: longitude,
          scale: 28,
          name: that.data.shopInfo.ShopName,
          address: that.data.shopInfo.MapLocationAddress
    });
  }
  
})